const express = require('express');
const app = express();
app.listen(3000, () => {
    console.log('服务器已开启');
})
const formidable = require('formidable');
const path = require('path');
const fs = require('fs');
const time = require('time-stamp');

let per = JSON.parse(fs.readFileSync(path.join(__dirname, 'persons.json'), 'utf-8'));

app.post('/register', (req, res) => {
    let form = formidable({
        uploadDir: path.join(__dirname, 'upload'),
        keepExtensions: true
    })
    form.parse(req, (err, fileds, files) => {
        console.log(files);
        let { username, password } = fileds;
        let tag = per.some(item => item.username == username);
        if (tag) {
            res.send('该用户已注册');
            fs.unlinkSync(files.photo.filepath);
        }
        else {
            let aPath = '/upload/' + files.photo.newFilename;
            per.push({ username: username, password: password, time: time('YYYY年MM月DD日 HH:mm:ss'), url: aPath });
            fs.writeFileSync(path.join(__dirname, 'persons.json'), JSON.stringify(per));
            res.send('注册成功');
        }
    })
})